package cn.tedu.util;

import cn.tedu.exception.ErrorException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * <pre>
 *
 * </pre>
 *
 * @author restyhap
 * @since 2019-11-04 11:07
 */
public class C3p0DBUtil {
    private static ComboPooledDataSource dataSource ;
    static  {
        dataSource = new ComboPooledDataSource();
    }

    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            System.out.println("当前数据库连接池对象是:"+conn);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                throw new ErrorException("获取Connection对象异常");
            } catch (ErrorException e1) {
                e1.printStackTrace();
            }
        }
        return conn ;
    }

    public static void close(Connection conn, Statement stat ){
        try {
            stat.close();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                throw new ErrorException("stat关闭异常");
            } catch (ErrorException e1) {
                e1.printStackTrace();
            }
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                try {
                    throw  new ErrorException("Conn关闭异常");
                } catch (ErrorException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void close (Connection conn, Statement stat, ResultSet rs){
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                throw new ErrorException("rs关闭异常");
            } catch (ErrorException e1) {
                e1.printStackTrace();
            }
        }finally {
            close(conn,stat);
        }
    }

    public void test1(){
        System.out.println(C3p0DBUtil.getConnection());

    }

}
